<?
class ScaffoldTool {
	public function find_text($text, $delimit_start = '`',	$delimit_end = '`')	{
		$start = strpos($text, $delimit_start);
		if ($start === false) return false;

		$end = strpos( substr($text, $start	+ 1), $delimit_end);
		if ($end === false)	return false;

		return substr (	$text, $start +	1, $end);
	}
}


class Scaffold {

	public $table = array();


	public function Scaffold($table){
		$this->table = $table;
		//Nlog::xmp($this->table);
		//echo $this->table['app_prefix'];
		include_once("class.table.php");
		$tbl = new Table();
		$this->tbl = $tbl;

	}




	####################################################
	# private                                          #
	####################################################


	private function title($name) {
		return trim($name);
	}


	private function heading($validation_check_is_empty_has = FALSE, $validation_check_isnt_key_val = FALSE) {
		$ret .= "<?php																				".PHP_EOL;
		$ret .= "	####################################################							".PHP_EOL;
		$ret .= "	# library import and object instance.              #							".PHP_EOL;
		$ret .= "	####################################################							".PHP_EOL;
		$ret .= "	include_once('./common.php');													".PHP_EOL;
		$ret .= "	include_once('./{$this->table['common_page']}');												".PHP_EOL;
		$ret .= "																					".PHP_EOL;
		$ret .= "																					".PHP_EOL;
		$ret .= "	####################################################							".PHP_EOL;
		$ret .= "	# request data(\$_POST AND \$_GET FROM \$_REQUEST)    #							".PHP_EOL;
		$ret .= "	####################################################							".PHP_EOL;
		$ret .= "	# REQUEST(get and post variables)												".PHP_EOL;
		$ret .= "	\$req = Request::clean_request(\$_REQUEST, FALSE);								".PHP_EOL;
		$ret .= "																					".PHP_EOL;
		$ret .= "	# GET(url variables)															".PHP_EOL;
		$ret .= "																					".PHP_EOL;
		$ret .= "	# POST(form variables)															".PHP_EOL;
		$ret .= "																					".PHP_EOL;
		$ret .= "	# Validation(special case validate)												".PHP_EOL;
		if($validation_check_is_empty_has){
			$ret .= "	//*/																		".PHP_EOL;
			$ret .= "	#@@todo : php server validation												".PHP_EOL;
			if($validation_check_isnt_key_val){
				$ret .= "	Validation::check_isnt_key_val(\$req, '{$this->table['pk_key']}');		".PHP_EOL;
			}
			$ret .= "	Validation::check_is_emptys(\$req);											".PHP_EOL;
			$ret .= "	//*/																		".PHP_EOL;
		}
		$ret .= "																					".PHP_EOL;
		$ret .= "	####################################################							".PHP_EOL;
		$ret .= "	# implementation                                   #							".PHP_EOL;
		$ret .= "	####################################################							".PHP_EOL;
		$ret .= "																					".PHP_EOL;
		return $ret;
	}


	private function heading_inner() {
		$ret .= "?>																					".PHP_EOL;
		$ret .= "<?php																				".PHP_EOL;
		$ret .= "	\$html->custom_header(TITLE);													".PHP_EOL;
		$ret .= "	\$html->custom_js(\"{\$JS}jquery.1.7.1.min.js\");								".PHP_EOL;
		$ret .= "	\$html->custom_js(\"{\$JS}lib.control.js\");									".PHP_EOL;
		$ret .= "	\$html->custom_css(\"{\$CSS}basic.css\");										".PHP_EOL;
		$ret .= "?>																					".PHP_EOL;
		return $ret;
	}


	private function heading_outer() {
		$ret .= "<?\$html->custom_body();?>															".PHP_EOL;
		$ret .= "<?=\$tmp->tiles_header()?>															".PHP_EOL;
		return $ret;
	}


	private function coomon_button() {
		$ret .= "	<span> | </span>									" . PHP_EOL;
		$ret .= "	<?=NTool::btn(\"back\", \"Mover.back();\");?>		" . PHP_EOL;
		$ret .= "	<?=NTool::btn(\"index\", \"Mover.home();\");?>		" . PHP_EOL;
		$ret .= "<?=\$tmp->tiles_footer()?>								" . PHP_EOL;
		$ret .= "<?php \$html->html_footer(); ?>						" . PHP_EOL;
		return $ret;
	}




	####################################################
	# public                                           #
	####################################################


	####################################
	# common                           #
	####################################


	public function common_page() {
		$ret = '';
		$ret .= "<?php															".PHP_EOL;
		$ret .= "	# 테이블 네임												".PHP_EOL;
		$ret .= "	\$ncrud['table_name'] = \"{$this->table['table_name']}\";	".PHP_EOL;
		$ret .= "?>																".PHP_EOL;
		return $ret;
	}


	####################################
	# ui                               #
	####################################


	public function list_page(){

		$ret = '';
		$column_array = array();
		$ret = $this->heading();

		# process
		$ret .= "	//*/																			".PHP_EOL;
		$ret .= "	\$pagenation_cfg = array(														".PHP_EOL;
		$ret .= "		'uri'               => \"{$this->table['list_page']}\",						".PHP_EOL;
		$ret .= "		'articles_count'    => 20,													".PHP_EOL;
		$ret .= "		'req_page'          => \$req['pagenation'],									".PHP_EOL;
		$ret .= "		'page_size'         => 6,													".PHP_EOL;
		$ret .= "		'articles_cnt'      => \$db->getcnts(\$ncrud['table_name']),				".PHP_EOL;
		$ret .= "		'style'             => Pagenation::\$style_default							".PHP_EOL;
		$ret .= "	);																				".PHP_EOL;
		$ret .= "	\$article = \$db->gets(															".PHP_EOL;
		$ret .= "		sprintf(\"SELECT * FROM `%s` ORDER BY `%s` DESC LIMIT %s, %s \",			".PHP_EOL;
		$ret .= "			\$ncrud['table_name'],													".PHP_EOL;
		$ret .= "			'{$this->table['pk_key']}',												".PHP_EOL;
		$ret .= "			\$pagenation_cfg['req_page'] * \$pagenation_cfg['articles_count'],		".PHP_EOL;
		$ret .= "			\$pagenation_cfg['articles_count']										".PHP_EOL;
		$ret .= "		)																			".PHP_EOL;
		$ret .= "	);																				".PHP_EOL;
		$ret .= "	\$pagenation = Pagenation::out(\$pagenation_cfg);								".PHP_EOL;
		$ret .= "	//*/																			".PHP_EOL;
		$ret .= $this->heading_inner();
		$ret .= $this->heading_outer();

		# title
		$ret .= sprintf("<h1>%slist</h1>", str_replace('_',' - ', $this->table['app_prefix'])).PHP_EOL;

		$ret .= $this->tbl->table_start();
			# th
			$ret .= $this->tbl->tr_start();
			foreach($this->table as $key => $val) {
				if (is_array($val)) {
					$column = $key ;
					$column_array[] = $key;
					$ret .= $this->tbl->th_start() . $this->tbl->tab(4, TRUE) . $this->title($column) . PHP_EOL . $this->tbl->th_end();
				}
			}
			$ret .= $this->tbl->th_start() . $this->tbl->tab(4, TRUE) . "action" . PHP_EOL . $this->tbl->th_end();
			$ret .= $this->tbl->tr_end();

			# td
			$ret .= $this->tbl->tab(2, TRUE) . "<?php if(\$pagenation_cfg['articles_cnt'] > 0):?>" . PHP_EOL;
				$ret .= $this->tbl->tab(3, TRUE) . "<?php foreach(\$article['res'] as \$key=>\$val):?>" . PHP_EOL;
				$ret .= $this->tbl->tab(1, TRUE) . $this->tbl->tr_start();
				foreach($column_array as $k=>$v){
					$ret .= $this->tbl->tab(1, TRUE) . $this->tbl->td_start() . $this->tbl->tab(6, TRUE) . "<?=\$val['$v']?>" . PHP_EOL . $this->tbl->tab(1, TRUE) . $this->tbl->td_end();
				}
				$ret .= $this->tbl->tab(1, TRUE) . $this->tbl->td_start() . $this->tbl->tab(6, TRUE);
				$ret .= "<a href=\"{$this->table['retrieve_page']}?{$this->table['pk_key']}=<?=\$val['{$this->table['pk_key']}']?>\">retrieve</a> | ";
				$ret .= "<a href=\"{$this->table['update_page']}?{$this->table['pk_key']}=<?=\$val['{$this->table['pk_key']}']?>\">update</a> | ";
				$ret .= "<a href=\"{$this->table['delete_page']}?{$this->table['pk_key']}=<?=\$val['{$this->table['pk_key']}']?>\">delete</a>";
				$ret .= PHP_EOL . $this->tbl->tab(1, TRUE) . $this->tbl->td_end();
				$ret .= $this->tbl->tab(1, TRUE) . $this->tbl->tr_end();
				$ret .= $this->tbl->tab(3, TRUE)."<?php endforeach;?>" . PHP_EOL;
			$ret .= $this->tbl->tab(2, TRUE)."<?php else:?>" . PHP_EOL;
				$ret .= $this->tbl->tab(1, TRUE) . $this->tbl->tr_start();
				$ret .= $this->tbl->tab(5, TRUE) . $this->tbl->td("no has data", sprintf("align=\"center\" colspan=\"%s\"",count($column_array)+1), TRUE, TRUE);
				$ret .= $this->tbl->tab(1, TRUE) . $this->tbl->tr_end();
			$ret .= $this->tbl->tab(2, TRUE)."<?php endif;?>" . PHP_EOL;
		$ret .= $this->tbl->table_end();

		# pagenation
		$ret .= "		<?=\$pagenation?>".PHP_EOL;

		# button
		$ret .= "	<?=NTool::btn(\"create\", \"Mover.href('./{$this->table['create_page']}');\");?>" . PHP_EOL;
		$ret .= $this->coomon_button();

		$ret = str_replace("	", "    ", $ret);
		return $ret;
	}


	public function create_page(){

		$ret = '';
		$column_array = array();
		$ret = $this->heading();

		# process
		$ret .= "	//*/																				".PHP_EOL;
		$ret .= "	//*/																				".PHP_EOL;
		$ret .= $this->heading_inner();
			$ret .= "<script type=\"text/javascript\" language=\"javascript\">							".PHP_EOL;
			$ret .= "//<![CDATA[																		".PHP_EOL;
			$ret .= "	var PageAction = {																".PHP_EOL;
			$ret .= "		validate:function(){														".PHP_EOL;
			$ret .= "			//*/																	".PHP_EOL;
			$ret .= "			/* @@todo : javascript validation */									".PHP_EOL;
			$ret .= "			var check = [															".PHP_EOL;
			$nn = 0;
			$ret_temp = "";
			foreach($this->table as $key => $val) {
				if (is_array($val)) {
					$column = $key ;
					if($column != $this->table['pk_key'] ){
						$column_array[] = $key;
						if($val['blob'] == 1){
							$ret_temp .= "				Validation.check_is_null_textarea(\"#{$this->title($column)}\", \"{$this->title($column)}을 입력 하세요.\")";
						} elseif($val['enum'] == 1) {
							$ret_temp .= "				Validation.check_is_do_not_chooesed(\"{$this->title($column)}\", \"{$this->title($column)}을 선택 하세요.\")";
						} else {
							$ret_temp .= "				Validation.check_is_null(\"#{$this->title($column)}\", \"{$this->title($column)}을 입력 하세요.\")";
						}
						if($nn != 0){
							$ret_temp .= ",[e]";
						}
					}
					$nn++;
				}
			}
			$ret_temp = substr($ret_temp, 0, -4);
			$ret_temp = str_replace('[e]', PHP_EOL, $ret_temp);
			$ret .= $ret_temp.PHP_EOL;
			$ret .= "			];																		".PHP_EOL;
			$ret .= "			return Validation.check(check);											".PHP_EOL;
			$ret .= "			//*/																	".PHP_EOL;
			$ret .= "		}																			".PHP_EOL;
			$ret .= "	}																				".PHP_EOL;
			$ret .= "//]]>																				".PHP_EOL;
			$ret .= "</script>																			".PHP_EOL;
		$ret .= $this->heading_outer();

		# title
		$ret .= sprintf("<h1>%screate</h1>", str_replace('_',' - ', $this->table['app_prefix'])).PHP_EOL;

		$ret .= "<form action=\"./{$this->table['create_proc_page']}\" id=\"formx\" name=\"formx\" method=\"post\" onsubmit=\"return PageAction.validate();\">".PHP_EOL;
		$ret .= $this->tbl->table_start();
			foreach($this->table as $key => $val) {
				if (is_array($val)) {
					$column = $key ;
					$ret .= $this->tbl->tr_start();

					if($column != $this->table['pk_key'] ){
						$column_array[] = $key;
						$ret .= $this->tbl->th_start();
						$ret .= $this->tbl->tab(4, TRUE) . $this->title($column) . PHP_EOL;
						$ret .= $this->tbl->th_end();
						$ret .= $this->tbl->td_start();
						if($val['blob'] == 1){
							$ret .= $this->tbl->tab(4, TRUE) . "<textarea name=\"$column\" id=\"$column\" rows=\"5\" cols=\"80\" ></textarea>" . PHP_EOL;
						} else if($val['enum'] == 1){
							foreach($val['enum_data'] as $k=>$v){
								$ret .= $this->tbl->tab(4, TRUE);
								$ret .= "<label><input type=\"radio\"name=\"$column\" id=\"$column\" value='{$v}' />{$v}</label>" . PHP_EOL;
							}
						} else {
							$ret .= $this->tbl->tab(4, TRUE) . "<input type=\"text\" name=\"$column\" id=\"$column\" />" . PHP_EOL;
						}
						$ret .= $this->tbl->td_end();
					} else {
						$column_array[] = $key;
						$ret .= $this->tbl->th_start();
						$ret .= $this->tbl->tab(4, TRUE) . $this->title($column) . PHP_EOL;
						$ret .= $this->tbl->th_end();
						$ret .= $this->tbl->td_start();
						$ret .= $this->tbl->tab(4, TRUE) . "PK" . PHP_EOL;
						$ret .= $this->tbl->td_end();
					}
					$ret .= $this->tbl->tr_end();
				}
			}
			$ret .= $this->tbl->tr_start();
			$ret .= "				<th colspan=\"2\">". PHP_EOL;
			$ret .= "					<?=NTool::submit_btn();?>". PHP_EOL;
			$ret .= "				</th>". PHP_EOL;
			$ret .= $this->tbl->tr_end();
		$ret .= $this->tbl->table_end();
		$ret .= "</form>".PHP_EOL;

		# button
		$ret .= "	<?=NTool::btn(\"list\", \"Mover.href('./{$this->table['list_page']}');\");?>" . PHP_EOL;
		$ret .= $this->coomon_button();

		$ret = str_replace("	", "    ", $ret);
		return $ret;
	}


	public function retrieve_page(){

		$ret = '';
		$column_array = array();
		$ret = $this->heading(TRUE,TRUE);

		# process
		$ret .= "	//*/																			".PHP_EOL;
		$ret .= "	\$article = \$db->get(															".PHP_EOL;
		$ret .= "		sprintf(\"SELECT * FROM `%s` WHERE `%s`= '%s'\",							".PHP_EOL;
		$ret .= "			\$ncrud['table_name'],													".PHP_EOL;
		$ret .= "			'{$this->table['pk_key']}',												".PHP_EOL;
		$ret .= "			\$req['{$this->table['pk_key']}']										".PHP_EOL;
		$ret .= "		)																			".PHP_EOL;
		$ret .= "	);																				".PHP_EOL;
		$ret .= "	//*/																			".PHP_EOL;
		$ret .= $this->heading_inner();
		$ret .= $this->heading_outer();

		# title
		$ret .= sprintf("<h1>%sretrieve</h1>", str_replace('_',' - ', $this->table['app_prefix'])).PHP_EOL;
		$ret .= $this->tbl->table_start();
			foreach($this->table as $key => $val) {
				if (is_array($val)) {
					$column = $key ;
					$ret .= $this->tbl->tr_start();
						$column_array[] = $key;
						$ret .= $this->tbl->th_start();
						$ret .= $this->tbl->tab(4, TRUE) . $this->title($column) . PHP_EOL;
						$ret .= $this->tbl->th_end();
						$ret .= $this->tbl->td_start();
						$ret .= $this->tbl->tab(4, TRUE) . "<?=StrFormat::element(\$article['res'][{$column}])?>" . PHP_EOL;
						$ret .= $this->tbl->td_end();
					$ret .= $this->tbl->tr_end();
				}
			}
		$ret .= $this->tbl->table_end();

		# button
		$ret .= "	<?=NTool::btn(\"update\", \"Mover.href('./{$this->table['update_page']}?{$this->table['pk_key']}={\$article['res']['{$this->table['pk_key']}']}');\")?>" . PHP_EOL;
		$ret .= "	<?=NTool::btn(\"delete\", \"Mover.href('./{$this->table['delete_page']}?{$this->table['pk_key']}={\$article['res']['{$this->table['pk_key']}']}');\")?>" . PHP_EOL;
		$ret .= "	<?=NTool::btn(\"list\", \"Mover.href('./{$this->table['list_page']}');\");?>" . PHP_EOL;
		$ret .= $this->coomon_button();

		$ret = str_replace("	", "    ", $ret);
		return $ret;
	}


	public function update_page(){

		$ret = '';
		$column_array = array();
		$ret = $this->heading(TRUE,TRUE);

		# process
		$ret .= "	//*/																			".PHP_EOL;
		$ret .= "	\$article = \$db->get(															".PHP_EOL;
		$ret .= "		sprintf(\"SELECT * FROM `%s` WHERE `%s`= '%s'\",							".PHP_EOL;
		$ret .= "			\$ncrud['table_name'],													".PHP_EOL;
		$ret .= "			'{$this->table['pk_key']}',												".PHP_EOL;
		$ret .= "			\$req['{$this->table['pk_key']}']										".PHP_EOL;
		$ret .= "		)																			".PHP_EOL;
		$ret .= "	);																				".PHP_EOL;
		$ret .= "	//*/																			".PHP_EOL;
		$ret .= $this->heading_inner();
			$ret .= "<script type=\"text/javascript\" language=\"javascript\">							".PHP_EOL;
			$ret .= "//<![CDATA[																		".PHP_EOL;
			$ret .= "	var PageAction = {																".PHP_EOL;
			$ret .= "		validate:function(){														".PHP_EOL;
			$ret .= "			//*/																	".PHP_EOL;
			$ret .= "			/* @@todo : javascript validation */									".PHP_EOL;
			$ret .= "			var check = [															".PHP_EOL;
			$nn = 0;
			$ret_temp = "";
			foreach($this->table as $key => $val) {
				if (is_array($val)) {
					$column = $key ;
					if($column != $this->table['pk_key'] ){
						if($val['blob'] == 1){
							$ret_temp .= "				Validation.check_is_null_textarea(\"#{$this->title($column)}\", \"{$this->title($column)}을 입력 하세요.\")";
						} elseif($val['enum'] == 1) {
							$ret_temp .= "				Validation.check_is_do_not_chooesed(\"{$this->title($column)}\", \"{$this->title($column)}을 선택 하세요.\")";
						} else {
							$ret_temp .= "				Validation.check_is_null(\"#{$this->title($column)}\", \"{$this->title($column)}을 입력 하세요.\")";
						}
						if($nn != 0){
							$ret_temp .= ",[e]";
						}
					}
					$nn++;
				}
			}
			$ret_temp = substr($ret_temp, 0, -4);
			$ret_temp = str_replace('[e]', PHP_EOL, $ret_temp);
			$ret .= $ret_temp.PHP_EOL;
			$ret .= "			];																		".PHP_EOL;
			$ret .= "			return Validation.check(check);											".PHP_EOL;
			$ret .= "			//*/																	".PHP_EOL;
			$ret .= "		}																			".PHP_EOL;
			$ret .= "	}																				".PHP_EOL;
			$ret .= "//]]>																				".PHP_EOL;
			$ret .= "</script>																			".PHP_EOL;

		$ret .= $this->heading_outer();

		# title
		$ret .= sprintf("<h1>%supdate</h1>", str_replace('_',' - ', $this->table['app_prefix'])).PHP_EOL;
		$ret .= "<form action=\"./{$this->table['update_proc_page']}\" id=\"formx\" name=\"formx\" method=\"post\" onsubmit=\"return PageAction.validate();\">".PHP_EOL;
		$ret .= $this->tbl->table_start();
			foreach($this->table as $key => $val) {
				if (is_array($val)) {
					$column = $key ;
					$ret .= $this->tbl->tr_start();

					if($column != $this->table['pk_key'] ){
						$column_array[] = $key;
						$ret .= $this->tbl->th_start();
						$ret .= $this->tbl->tab(4, TRUE) . $this->title($column) . PHP_EOL;
						$ret .= $this->tbl->th_end();
						$ret .= $this->tbl->td_start();
						if($val['blob'] == 1){
							$ret .= $this->tbl->tab(4, TRUE) . "<textarea name=\"$column\" id=\"$column\" rows=\"5\" cols=\"80\" ><?=\$article['res'][{$column}]?></textarea>" . PHP_EOL;
						} else if($val['enum'] == 1){
							foreach($val['enum_data'] as $k=>$v){
								$ret .= $this->tbl->tab(4, TRUE);
								$ret .= "<label><input type=\"radio\"name=\"$column\" id=\"$column\" value='{$v}' <?=NForm::checked(\$article['res'][{$column}], \"{$v}\")?> />{$v}</label>" . PHP_EOL;
							}
						} else {
							$ret .= $this->tbl->tab(4, TRUE) . "<input type=\"text\" name=\"$column\" id=\"$column\" value='<?=\$article['res'][{$column}]?>' />" . PHP_EOL;
						}
						$ret .= $this->tbl->td_end();
					} else {
						$column_array[] = $key;
						$ret .= $this->tbl->th_start();
						$ret .= $this->tbl->tab(4, TRUE) . $this->title($column) . PHP_EOL;
						$ret .= $this->tbl->th_end();
						$ret .= $this->tbl->td_start();
						$ret .= $this->tbl->tab(4, TRUE) . "<input type='hidden' name='$column' id='$column' value='<?=\$article['res'][{$column}]?>' /><?=\$article['res'][{$column}]?>" . PHP_EOL;
						$ret .= $this->tbl->td_end();
					}
					$ret .= $this->tbl->tr_end();
				}
			}
			$ret .= $this->tbl->tr_start();
			$ret .= "				<th colspan=\"2\">". PHP_EOL;
			$ret .= "					<?=NTool::submit_btn();?>". PHP_EOL;
			$ret .= "				</th>". PHP_EOL;
			$ret .= $this->tbl->tr_end();
		$ret .= $this->tbl->table_end();
		$ret .= "</form>".PHP_EOL;

		# button
		$ret .= "	<?=NTool::btn(\"retrieve\", \"Mover.href('./{$this->table['retrieve_page']}?{$this->table['pk_key']}={\$article['res']['{$this->table['pk_key']}']}');\")?>" . PHP_EOL;
		$ret .= "	<?=NTool::btn(\"list\", \"Mover.href('./{$this->table['list_page']}');\");?>" . PHP_EOL;
		$ret .= $this->coomon_button();

		$ret = str_replace("	", "    ", $ret);
		return $ret;
	}


	public function delete_page(){

		$ret = '';
		$column_array = array();
		$ret = $this->heading(TRUE,TRUE);

		# process
		$ret .= $this->heading_inner();
		$ret .= $this->heading_outer();

		# title
		$ret .= sprintf("<h1>%sdelete</h1>", str_replace('_',' - ', $this->table['app_prefix'])).PHP_EOL;
		$ret .= "<form action=\"./{$this->table['delete_proc_page']}\" id=\"formx\" name=\"formx\" method=\"post\" onsubmit=\"return PageAction.validate();\">".PHP_EOL;
		$ret .= $this->tbl->table_start();
		$ret .= $this->tbl->tr_start();
		$ret .= $this->tbl->td_start();
		$ret .= $this->tbl->tab(4, TRUE) . "<input type='hidden' name='{$this->table['pk_key']}' id='{$this->table['pk_key']}' value='<?=\$req[{$this->table['pk_key']}]?>' />".PHP_EOL;
		$ret .= $this->tbl->tab(4, TRUE) . "I will put my decision in action soon that delete <strong>{$this->table['pk_key']}</strong> column has value to <strong><?=\$req[{$this->table['pk_key']}]?></strong>".PHP_EOL;
		$ret .= $this->tbl->tab(4, TRUE) . "if you want click to confirm button" . PHP_EOL;
		$ret .= $this->tbl->td_end();
		$ret .= $this->tbl->tr_end();
		$ret .= $this->tbl->tr_start();
		$ret .= "				<th>". PHP_EOL;
		$ret .= "					<?=NTool::submit_btn();?>". PHP_EOL;
		$ret .= "				</th>". PHP_EOL;
		$ret .= $this->tbl->tr_end();
		$ret .= $this->tbl->table_end();
		$ret .= "</form>".PHP_EOL;

		# button
		$ret .= "	<?=NTool::btn(\"retrieve\", \"Mover.href('./{$this->table['retrieve_page']}?{$this->table['pk_key']}={\$req['{$this->table['pk_key']}']}');\")?>" . PHP_EOL;
		$ret .= "	<?=NTool::btn(\"update\", \"Mover.href('./{$this->table['update_page']}?{$this->table['pk_key']}={\$req['{$this->table['pk_key']}']}');\")?>" . PHP_EOL;
		$ret .= "	<?=NTool::btn(\"list\", \"Mover.href('./{$this->table['list_page']}');\");?>" . PHP_EOL;
		$ret .= $this->coomon_button();

		$ret = str_replace("	", "    ", $ret);
		return $ret;
	}


	####################################
	# proc                             #
	####################################


	public function create_proc_page(){
		$ret = '';
		$column_array = array();
		$ret = $this->heading(TRUE,FALSE);

		# process
		$ret .= "	//*/																				".PHP_EOL;
		$ret .= "	\$ret = \$db->que(																	".PHP_EOL;
			$query = "\t\tsprintf(\"INSERT INTO `%s` VALUES (";
			$field = "\t\t\t\$ncrud['table_name'],[e]";
			foreach($this->table as $key => $val) {
				if (is_array($val)) {
					$column = $key ;
					if($column != $this->table['pk_key'] ){
						$column_array[] = $key;
						$query .= ",'%s'";
						$field .= "\t\t\t\$req['{$column}'],[e]";
					} else {
						$query .= "NULL";
						$field .= "";
					}
				}
			}
			$query .= ")\",".PHP_EOL;
			$field = substr($field, 0, -4);
			$field = str_replace('[e]', PHP_EOL, $field);
			$field .= PHP_EOL."\t\t)".PHP_EOL;
			$ret .= $query.$field;
		$ret .= "	);																					".PHP_EOL;
		$ret .= "	//*/																				".PHP_EOL;
		$ret .= $this->heading_inner();
		$ret .= $this->heading_outer();

		# title
		$ret .= sprintf("<h1>%screate_proc</h1>", str_replace('_',' - ', $this->table['app_prefix'])).PHP_EOL;

		$ret .= "	<?php if(\$ret['has']): ?>			".PHP_EOL;
		$ret .= "		success							".PHP_EOL;
		$ret .= "	<?php else: ?>						".PHP_EOL;
		$ret .= "		failed							".PHP_EOL;
		$ret .= "	<?php endif; ?>						".PHP_EOL;
		$ret .= "	<br />								".PHP_EOL;

		# button
		$ret .= "	<?=NTool::btn(\"list\", \"Mover.href('./{$this->table['list_page']}');\");?>" . PHP_EOL;
		$ret .= $this->coomon_button();

		$ret = str_replace("	", "    ", $ret);
		return $ret;
	}

	public function update_proc_page() {
		$ret = '';
		$column_array = array();
		$ret = $this->heading(TRUE,FALSE);

		# process
		$ret .= "	//*/																				".PHP_EOL;
		$ret .= "	\$ret = \$db->que(																	".PHP_EOL;
			$query = "\t\tsprintf(\"UPDATE `%s` SET ";
			$field = "\t\t\t\$ncrud['table_name'],[e]";
			foreach($this->table as $key => $val) {
				if (is_array($val)) {
					$column = $key ;
					if($column != $this->table['pk_key'] ){
						$column_array[] = $key;
						$query .= "`$column`='%s', ";
						$field .= "\t\t\t\$req['{$column}'],[e]";
					} else {
					}
				}
			}
			$query = substr($query, 0, -2);
			$query .= " WHERE `{$this->table['pk_key']}`='%s'\",".PHP_EOL;
			$field .= "\t\t\t\$req['{$this->table['pk_key']}'],[e]";
			$field = substr($field, 0, -4);
			$field = str_replace('[e]', PHP_EOL, $field);
			$field .= PHP_EOL."\t\t)".PHP_EOL;
			$ret .= $query.$field;
		$ret .= "	);																					".PHP_EOL;
		$ret .= "	//*/																				".PHP_EOL;
		$ret .= $this->heading_inner();
		$ret .= $this->heading_outer();

		# title
		$ret .= sprintf("<h1>%supdate_proc</h1>", str_replace('_',' - ', $this->table['app_prefix'])).PHP_EOL;
		$ret .= "	<?php if(\$ret['has']): ?>			".PHP_EOL;
		$ret .= "		success							".PHP_EOL;
		$ret .= "	<?php else: ?>						".PHP_EOL;
		$ret .= "		failed							".PHP_EOL;
		$ret .= "	<?php endif; ?>						".PHP_EOL;
		$ret .= "	<br />								".PHP_EOL;

		# button
		$ret .= "	<?=NTool::btn(\"list\", \"Mover.href('./{$this->table['list_page']}');\");?>" . PHP_EOL;
		$ret .= $this->coomon_button();

		$ret = str_replace("	", "    ", $ret);
		return $ret;
	}

	public function delete_proc_page() {
		$ret = '';
		$column_array = array();
		$ret = $this->heading(TRUE,FALSE);

		# process
		$ret .= "	//*/																				".PHP_EOL;
		$ret .= "	\$ret = \$db->que(																	".PHP_EOL;
			$query = "\t\tsprintf(\"DELETE FROM `%s` WHERE `{$this->table['pk_key']}`='%s';";
			$field = "\t\t\t\$ncrud['table_name'],[e]";
			$field .= "\t\t\t\$req['{$this->table['pk_key']}'],[e]";
			$query .= "\",".PHP_EOL;
			$field = substr($field, 0, -4);
			$field = str_replace('[e]', PHP_EOL, $field);
			$field .= PHP_EOL."\t\t)".PHP_EOL;
			$ret .= $query.$field;
		$ret .= "	);																					".PHP_EOL;
		$ret .= "	//*/																				".PHP_EOL;
		$ret .= $this->heading_inner();
		$ret .= $this->heading_outer();

		# title
		$ret .= sprintf("<h1>%sdelete_proc</h1>", str_replace('_',' - ', $this->table['app_prefix'])).PHP_EOL;

		$ret .= "	<?php if(\$ret['has'] > 0): ?>		".PHP_EOL;
		$ret .= "		success							".PHP_EOL;
		$ret .= "	<?php else: ?>						".PHP_EOL;
		$ret .= "		failed							".PHP_EOL;
		$ret .= "	<?php endif; ?>						".PHP_EOL;
		$ret .= "	<br />								".PHP_EOL;

		# button
		$ret .= "	<?=NTool::btn(\"list\", \"Mover.href('./{$this->table['list_page']}');\");?>" . PHP_EOL;
		$ret .= $this->coomon_button();

		$ret = str_replace("	", "    ", $ret);
		return $ret;
	}

}